package test_achieve.courierapp;

import constant.Constant;
import cucumber.api.java.en.Given;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import util.tools.CommonTools;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by chenkuo on 2017/11/10.
 */
public class CourierPrice {

    @Given("^抢单界面，确定订单总额：(.*)$")
    public void 抢单界面确定订单总额(String mom) throws Throwable {
        if (Constant.interfaceGrabStatus){
            System.out.println("接口抢单，不需要抢单界面，确定订单总额");
        }else {
            String moneyString = Constant.driverCourier.findElement(By.id("price_head")).getText();
//        String moneyString = "￥42";
            Pattern pattern = Pattern.compile("^￥\\d+\\+?\\d?");
            Matcher matcher = pattern.matcher(moneyString);
            matcher.find();
            String s = matcher.group(0);
            String money1 = CommonTools.cutEnd(s, "￥");
            String money[] = money1.split("\\+");
            Integer sum = 0;
            for (int i = 0; i < money.length; i++) {
                sum = sum + Integer.parseInt(money[i]);
            }
            System.out.println(sum);
            Assert.assertEquals(mom, sum.toString());
        }
    }

    public static void main(String[] args) {
        CourierPrice courierPrice = new CourierPrice();
        try {
            courierPrice.抢单界面确定订单总额("42");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }


    @Given("^待取件详情界面，确定订单金额：(.*)$")
    public void 待取件详情界面确定订单金额(String mom) throws Throwable {
        String moneyString = Constant.driverCourier.findElement(By.id("tv_orderPrice")).getText();
        String money = CommonTools.splitNumber(moneyString,"￥",1);
        System.out.println("待取件详情界面，确定订单金额:"+money);
        Assert.assertEquals(mom,money);

    }

    @Given("^待收件详情界面，确定付款方式：(.*)$")
    public void 待收件详情界面确定付款方式在线支付(String mom) throws Throwable {
        String moneyString = Constant.driverCourier.findElement(By.id("paystyle_txt")).getText();
        String money = CommonTools.cutEnd(moneyString,"付款方式:");
        System.out.println("确定付款方式："+money);
        Assert.assertEquals(mom,money);
    }

    @Given("^闪送完成界面，确定本次收入金额：(.*)$")
    public void 闪送完成界面确定本次收入金额本次收入(String mom) throws Throwable {
        String moneyString = Constant.driverCourier.findElement(By.id("incoming")).getText();
        String money = CommonTools.cutFront(moneyString,"元");
        System.out.println("闪送完成界面，确定本次收入金额:"+money);
        Assert.assertEquals(mom,money);
    }

    @Given("^今日订单中，判断订单总额：(.*)$")
    public void 今日订单中判断订单总额订单总额(String mom) throws Throwable {
        List<WebElement> num= Constant.driverCourier.findElements(By.id("tasknum_txt"));
        String numbuter = num.get(0).getText();
        String orderNumbuter = Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1);
        if (!(numbuter.equals(orderNumbuter))){
            点击刷新按钮();
            Thread.sleep(5000);
        }
        List<WebElement> btn= Constant.driverCourier.findElements(By.id("price_head"));
        String moneyString = CommonTools.splitNumber(btn.get(0).getText(),"￥",1);
        System.out.println("今日订单，判断订单总额："+moneyString);
        Assert.assertEquals(mom,moneyString);

    }

    @Given("^点击刷新按钮$")
    public void 点击刷新按钮() throws Throwable {
        Constant.driverCourier.findElement(By.id("img_refresh")).click();

    }

    @Given("^代取件详情界面判断订单总额：(.*)$")//只有一取多投会有此金额显示
    public void 代取件详情界面判断订单总额(String mon) throws Throwable {
        String moneyString = Constant.driverCourier.findElement(By.id("tv_orderPrice")).getText();
        String money = CommonTools.splitNumber(moneyString,"￥",1);
        Assert.assertEquals(money,mon);
    }


    @Given("^确认客服流单后闪送员账户余额$")
    public void 确认客服流单后闪送员账户余额() throws Throwable {
        Oneself my = new Oneself();
        my.进入我的闪送页面();
        String moneyString = Constant.driverCourier.findElement(By.id("total_money")).getText();
        float money = Float.parseFloat(CommonTools.cutFront(moneyString,"元"));
        Assert.assertEquals(money,Constant.courierBalance,0.00001);

    }
}
